import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '../utils/localData.js'
Vue.use(VueRouter)



import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import User from '../views/User.vue'
import UserEdit from '../views/UserEdit.vue'
import Home from '../views/Home.vue'
import Article from '../views/Article.vue'
import UserFollow from '../views/UserFollow.vue'
import Collect from '../views/userCollect.vue'
import UserComment from '../views/UserComment.vue'
import ArticleComment from '../views/ArticleComment.vue'
import Category from '../views/Category.vue'
import Search from '../views/Search.vue'
const routes = [
	{path: '/' , redirect: '/home'},
	{meta: {keepAlive: true} , path: '/home' , component: Home},
	{path: '/home/category' , component: Category},
	{path: '/home/search' , component: Search},
	{path: '/article/:id', component: Article , name: 'article'},
	{path: '/article_comment/:id' , component: ArticleComment},
	{path: '/login' , component: Login , name: 'login'},
	{path: '/register' , component: Register},
	{meta: {checked: true} , path: '/user' , component: User},
	{meta: {checked: true} , path: '/user_edit' , component: UserEdit},
	{meta: {checked: true} , path: '/user_follow' , component: UserFollow},
	{meta: {checked: true} , path: '/user_collect' , component: Collect},
	{meta: {checked: true} , path: '/user_comment' , component: UserComment},
]

const router = new VueRouter({
  routes,
  mode: 'history',
})

//路由守卫应当在实例中添加
/* 
	to: 目标地址
	from: 源地址
	next: 放行（是一个函数）
 
 */
router.beforeEach((to , from , next) => {
	//const pathArr = ['/user' , '/user_edit'];
	
	if(to.meta.checked){
		if(getToken()){
			next();
		}else{
			console.log("token")
			next({ path: '/login'})
		}
	
	}else{
		next();
	}
})

export default router
